﻿using HookupAppForm.Dto;
using HookupAppForm.MyUserControl;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HookupAppForm
{
    public partial class FormBlock : Form
    {
        private string act = "api/services/app/HKP_Block/";
        private HKP_BlockDto Item { get; set; }
        private APIClient client { get; set; }
        private string Building { get; set; }
        private string Floor { get; set; }
        private int X { get; set; }
        private int Y { get; set; }
        public FormBlock(HKP_BlockDto item, APIClient _client, string building, string floor, int x = 0, int y = 0)
        {
            InitializeComponent();
            Item = item;
            client = _client;
            Building = building;
            Floor = floor;
            X = x;
            Y = y;
        }

        private void FormBlock_Load(object sender, EventArgs e)
        {
            ucDpdBuilding.Init("Building", client.RemoteHost);
            ucDpdBuilding.selectedChangeEvent += new UserControlDropdown.SelectedChangeDelegate(UcDpdBuildingSelectedChange);
            if (Item != null)
            {
                txtBlockName.Text = Item.BlockName;
                ucDpdBuilding.SetText(Item.Building);
                ucDpdFloor.SetText(Item.Floor);
                numLength.Value= Item.Length;
                numWidth.Value= Item.Width;
                numX.Value = Item.X;
                numY.Value = Item.Y;
                numOrderIndex.Value = Item.OrderIndex;
            }
            else
            {
                numX.Value = X;
                numY.Value = Y;
                ucDpdBuilding.SetText(Building);
                ucDpdFloor.SetText(Floor);
            }
            LoadData();
        }
        private void UcDpdBuildingSelectedChange(object sender, EventArgs e)
        {
            ucDpdFloor.Init("Building", client.RemoteHost, Convert.ToInt32(ucDpdBuilding.selectedValue));
        }
        private void LoadData()
        {
            string url = string.Format(act + "GetList");
            var result = client.Get(url);
            ItemsDto<HKP_BlockDto> res = JsonConvert.DeserializeObject<ItemsDto<HKP_BlockDto>>(result);
            dataGridViewM.DataSource = res?.result?.items;
            FromSetting[] fromSettings = new FromSetting[10];
            fromSettings[2] = new FromSetting { FieldDisplay = "区块名称" };
            fromSettings[3] = new FromSetting { FieldDisplay = "长度" };
            fromSettings[4] = new FromSetting { FieldDisplay = "宽度" };
            fromSettings[5] = new FromSetting { FieldDisplay = "X坐标" };
            fromSettings[6] = new FromSetting { FieldDisplay = "Y坐标" };            
            fromSettings[7] = new FromSetting { FieldDisplay = "建筑" };
            fromSettings[8] = new FromSetting { FieldDisplay = "楼层" };
            Utils.SetHeader(dataGridViewM, fromSettings);
        }
        private void SaveToolStripButton_Click(object sender, EventArgs e)
        {
            string url = act + (Item != null ? "Update" : "Create");
            string result = string.Empty;
            string jsons = string.Empty;
            if (Item != null)
            {
                HKP_BlockDto dto = new HKP_BlockDto();
                dto.Id = Item.Id;
                dto.BlockName = txtBlockName.Text;
                dto.Building = ucDpdBuilding.selectedText;
                dto.Floor = ucDpdFloor.selectedText;
                dto.Length = numLength.Value;
                dto.Width = numWidth.Value;
                dto.X = numX.Value;
                dto.Y = numY.Value;
                jsons = JsonConvert.SerializeObject(dto);
                result = client.Put(url, jsons);
            }
            else
            {
                CreateHKP_BlockDto dto = new CreateHKP_BlockDto();
                dto.BlockName = txtBlockName.Text;
                dto.Building = ucDpdBuilding.selectedText;
                dto.Floor = ucDpdFloor.selectedText;
                dto.Length = numLength.Value;
                dto.Width = numWidth.Value;
                dto.X = numX.Value;
                dto.Y = numY.Value;
                jsons = JsonConvert.SerializeObject(dto);
                result = client.Post(url, jsons);                
            }
            
            if (Utils.ExceptionHandler(result) == null)
            {
                Utils.SysMsg("保存成功！");
                var resp = Utils.SuccessHandler(result);
                Item = JsonConvert.DeserializeObject<HKP_BlockDto>(resp.ToString());
                LoadData();
            }
        }

        private void NewToolStripButton_Click(object sender, EventArgs e)
        {
            if (Item != null)
            {
                txtBlockName.Text = "";
                ucDpdBuilding.SetText("");
                ucDpdFloor.SetText("");
                numLength.Value = 0;
                numWidth.Value = 0;
                numX.Value = 0;
                numY.Value = 0;
                numOrderIndex.Value = 0;
                Item = null;                
            }
            copyToolStripButton.Enabled = false;
        }

        private void CopyToolStripButton_Click(object sender, EventArgs e)
        {
            if (Item != null)
            {
                string url = act + "Create";
                string jsons = JsonConvert.SerializeObject(Item);
                string result = client.Post(url, jsons);
                if (Utils.ExceptionHandler(result) == null)
                {
                    Utils.SysMsg("保存成功！");
                }
                LoadData();
            }
        }

        private void DataGridViewM_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Item = dataGridViewM.CurrentRow.DataBoundItem as HKP_BlockDto;
            if (Item != null)
            {
                txtBlockName.Text = Item.BlockName;
                ucDpdBuilding.SetText(Item.Building);
                ucDpdFloor.SetText(Item.Floor);
                numLength.Value = Item.Length;
                numWidth.Value = Item.Width;
                numX.Value = Item.X;
                numY.Value = Item.Y;
                numOrderIndex.Value = Item.OrderIndex;
                copyToolStripButton.Enabled = true;
            }
        }

        private void DelToolStripButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除？", "系统", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                if (Item != null)
                {
                    string url = string.Format(act + "Delete?Id={0}", Item.Id);
                    string result = client.Delete(url);
                    if (Utils.ExceptionHandler(result) == null)
                    {
                        LoadData();
                    }
                }
            }
        }

        private void SubMainToolStripButton_Click(object sender, EventArgs e)
        {
            FormSubmain form = new FormSubmain(null, client);
            form.ShowDialog();
        }

        private void TakeOffToolStripButton_Click(object sender, EventArgs e)
        {
            FormTakeOff form = new FormTakeOff(null, client);
            form.ShowDialog();
        }
        internal void SetLoc(int x, int y )
        {
            try
            {
                numX.Value = x;
                numY.Value = y; 
            }
            catch (Exception)
            {
            }
        }
    }
}
